package com.api.oa.controller;


import com.api.oa.model.entity.YQRequest;
import com.api.oa.model.params.YQRequestParams;
import com.api.oa.services.YQRequestService;
import com.api.shopapi.controller.BaseController;
import com.api.shopapi.entity.MyResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/yq/yQRequest")
public class YQRequestController extends BaseController {

    @Autowired
    private YQRequestService iYQRequestService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public MyResult Add(@RequestBody YQRequest entity) {
        try {
            return iYQRequestService.add(entity) == 1 ? success(entity) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(e.getMessage());
        }

    }


    @RequestMapping(value = "/remove/{id}", method = RequestMethod.POST)
    public MyResult remove(@PathVariable String id) {
        try {
            iYQRequestService.remove(id);
            return success("删除成功!");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }



    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public MyResult update(@RequestBody YQRequest entity) {
        try {
            Integer update = iYQRequestService.update(entity);
            return update == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    /**
     * 政府申请长湖
     * @param entity
     * @return
     */
    @RequestMapping(value = "/request/gov", method = RequestMethod.POST)
    public MyResult requestByGov(@RequestBody YQRequest entity) {
        try {
            Integer update = iYQRequestService.requestGov(entity);
            return update == 1 ? success("操作成功") : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }


    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public MyResult list(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(required = false) String insCode,@RequestParam(required = false)String idCard) {
        try {
            return success(iYQRequestService.list(insCode,idCard, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 申请 机构 移动端
     *
     * @param pageIndex
     * @param pageSize
     * @param idCard
     * @param insUserId
     * @return
     */
    @RequestMapping(value = "/queryByIdCard", method = RequestMethod.GET)
    public MyResult queryByIdCard(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(defaultValue = "") String idCard, @RequestParam(required = false) Long insUserId,
                                  @RequestParam(required = false) Long status,@RequestParam(required = false) Long assUserId) {
        try {
            return success(iYQRequestService.queryByIdCardIns(idCard, insUserId, status, assUserId,pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 移动端 政府的
     * @param pageIndex
     * @param pageSize
     * @param idCard
     * @param insUserId
     * @param isAss
     * @return
     */
    @RequestMapping(value = "/queryByIdCardGov", method = RequestMethod.GET)
    public MyResult queryByIdCardGov(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(defaultValue = "") String idCard, @RequestParam(required = false) Long insUserId,
                                     @RequestParam(defaultValue = "false") Boolean isAss,@RequestParam(required = false) Long assUserId) {
        try {
            return success(iYQRequestService.queryByIdCardGov(idCard, insUserId, isAss,assUserId, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "/one/{id}", method = RequestMethod.GET)
    public MyResult one(@PathVariable String id) {
        try {
            return success(id == null ? null : iYQRequestService.one(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * 我的申请
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/myRequest/{id}", method = RequestMethod.GET)
    public MyResult myRequest(@PathVariable Long id) {
        try {
            return success(iYQRequestService.findOneByUserId(id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }

    /**
     * //机构评定患者
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/assessment", method = RequestMethod.POST)
    public MyResult assessmentIns(@RequestBody YQRequestParams entity) {
        try {
            return iYQRequestService.assessmentIns(entity) == 1 ? success(entity) : failed("操作失败");
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }

    /**
     * //机构更新患者坐标
     *
     * @return
     */
    @RequestMapping(value = "/update/loc/{id}", method = RequestMethod.GET)
    public MyResult updateLocationByIns(@PathVariable String id,@RequestParam(defaultValue = "0") Double lat,@RequestParam(defaultValue = "0") Double lng) {
        try {
            return success(iYQRequestService.updateLocation(lat,lng,id));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }

    }



    @RequestMapping(value = "/requests", method = RequestMethod.GET)
    public MyResult requests(@RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(required = false) String insCode) {
        try {
            return success(iYQRequestService.request(insCode, pageIndex, pageSize));
        } catch (Exception e) {
            return anomalous(getExceptionMsg(e));
        }
    }


}
